/**
 * 
 */
package sailing.ui.model;

import java.util.List;

import org.jdesktop.application.SingleFrameApplication;

import sailing.entity.BankDepositsAccount;
import sailing.entity.CashVoucher;
import sailing.entity.ReceivableDetail;
import sailing.managers.BankDepositsAccountManager;
import sailing.managers.ReceivableDetailManager;
import sailing.utils.Utils;
import cjlite.utils.Lists;

/**
 * @author simon
 * 
 */
public class ReceivableDetailTableModel extends SailingTableModel {

	public ReceivableDetailTableModel(SingleFrameApplication application) {
		super(application);
		// TODO Auto-generated constructor stub
	}

	String[] columnName = { "全称", "项目款发票开出时间", "简述", "车间", "金额", "项目款收入时间", "备注", "编辑操作" };
	private boolean showEditBtn = false;

	private List<ReceivableDetail> receivableDetailList = Lists.newArrayList();

	@Override
	public void fireTableDataChanged() {
		this.updateModel();
		super.fireTableDataChanged();
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see javax.swing.table.TableModel#getRowCount()
	 */
	@Override
	public int getRowCount() {
		return receivableDetailList.size();
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see javax.swing.table.TableModel#getColumnCount()
	 */
	@Override
	public int getColumnCount() {
		return showEditBtn? 8:7;
	}

	@Override
	public String getColumnName(int column) {
		return columnName[column];
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see javax.swing.table.TableModel#getValueAt(int, int)
	 */
	@Override
	public Object getValueAt(int rowIndex, int columnIndex) {
		if (rowIndex >= this.receivableDetailList.size()) {
			return "";
		}
		ReceivableDetail b = this.receivableDetailList.get(rowIndex);
		switch (columnIndex) {
		case 0:
			return b.getReceivableFullDesc();
		case 1:
			return Utils.dateFormat(b.getInvoiceDateTime());
		case 2:
			return b.getReceivableSummary();
		case 3:
			return b.getWorkshop();
		case 4:
			return b.getReceivableAccountAmount();
		case 5:
			return Utils.dateFormat(b.getIncomeDateTime());
		case 6:
			return b.getReceivableRemark();
		case 7:
			return b;
		}
		return "";
	}

	@Override
	public boolean isCellEditable(int rowIndex, int columnIndex) {
		return showEditBtn && columnIndex == 2;
	}

	@Override
	public void updateModel() {
		ReceivableDetailManager bm = this.getApplication().getContext().getIoc().getInstance(ReceivableDetailManager.class);
		receivableDetailList.clear();
		receivableDetailList.addAll(bm.getAllReceivableDetailList());
	}

	public void showEditBtn(boolean selected) {
		showEditBtn = selected;
	}
	
	public void updateModel(List<ReceivableDetail> cvList) {
		receivableDetailList.clear();
		receivableDetailList.addAll(cvList);
		super.fireTableDataChanged();
	}

	public List<ReceivableDetail> getDataList() {
		return this.receivableDetailList;
	}

}
